package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.ddyx.entity.WxUser;
import com.ddyx.entity.user.UStore;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.mapper.user.UStoreMapper;
import com.ddyx.service.IGuideService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MQUtil;
import com.ddyx.util.PropertiesUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.template.KeyWord;
import com.ddyx.util.template.TemplateMessageParam;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class GuideServiceImpl implements IGuideService{
	@Resource
	UStoreMapper usMapper;
	@Resource
	JmsTemplate jmsTemplate;
	@Resource
	Destination tmpMsgDestination;
	@Resource
	WxUserMapper wxUserMapper;
	private static ObjectMapper jsonMapper = new ObjectMapper();
	/*
	 * 导购服务通知
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void guideServiceNotification(RequestForm param) throws Exception {
		if (param.getData() != null) {
			List list = new ArrayList<>();
			TemplateMessageParam tmpMsgParam = new TemplateMessageParam();
			Map<String, Object> keyword = new HashMap<>();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			String wxUid = (String) paramMap.get("wxUid");
			String employeName = (String) paramMap.get("employeName");
			String storeNo = (String) paramMap.get("storeNo");
			String formId = (String) paramMap.get("formId");

			/*Store store = new Store();
			store.setStoreNo(storeNo);
			store = storeMapper.selectOne(store);*/
			UStore store = new UStore();
			store.setStoreNo(storeNo);
			store = usMapper.selectOne(store);
			
			WxUser wxUser = new WxUser();
			wxUser.setWxUid(wxUid);
			wxUser = wxUserMapper.selectOne(wxUser);
			
			if(wxUser!=null&&store!=null) {
				list.add(new KeyWord(employeName, EventTag.BLACK));
				list.add(new KeyWord(store.getStoreName(), EventTag.BLACK));
				list.add(new KeyWord(store.getLocation(), EventTag.BLACK));
				list.add(new KeyWord(store.getPhoneNumber(), EventTag.BLACK));
				int i=1;
				for (Object o : list) {
					keyword.put("keyword"+i, o);
					i++;
				}
				tmpMsgParam.setTouser(wxUser.getOpenId());
				tmpMsgParam.setForm_id(formId);
				tmpMsgParam.setTemplate_id(PropertiesUtil.getProperty(EventTag.GUIDETEMP));
				tmpMsgParam.setData(keyword);
				tmpMsgParam.setEmphasis_keyword("keyword"+2+".DATA");
				
				String json = jsonMapper.writeValueAsString(tmpMsgParam);
				//MQ异步发送 
				MQUtil.sendJsonMsg(json, jmsTemplate, tmpMsgDestination);
				
			}
			

		}else{
			log.error("参数有误");
			System.out.println("参数有误");
		}
	}
	
}
